// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#pragma once

#include <pollux/functions/macros.h>
#include <pollux/functions/prestosql/json/simd_json_extractor.h>
#include <pollux/functions/prestosql/json/simd_json_wrapper.h>
#include <pollux/functions/prestosql/types/json_type.h>

namespace kumo::pollux::functions {
    template<typename TExec>
    struct FailFunction {
        POLLUX_DEFINE_FUNCTION_TYPES(TExec);

        MELON_ALWAYS_INLINE Status
        call(out_type<UnknownValue> & /*result*/, const arg_type<Varchar> &message) {
            if (threadSkipErrorDetails()) {
                return Status::UserError();
            }

            return Status::UserError("{}", message);
        }

        MELON_ALWAYS_INLINE Status call(
            out_type<UnknownValue> &result,
            const arg_type<int32_t> & /*errorCode*/,
            const arg_type<Varchar> &message) {
            return call(result, message);
        }
    };

    template<typename TExec>
    struct FailFromJsonFunction {
        POLLUX_DEFINE_FUNCTION_TYPES(TExec);

        MELON_ALWAYS_INLINE Status
        call(out_type<UnknownValue> & /*result*/, const arg_type<Json> &json) {
            if (threadSkipErrorDetails()) {
                return Status::UserError();
            }

            // Extract 'message' field from 'json'.
            std::string_view message;
            if (extractMessage(json, message) != simdjson::SUCCESS) {
                return Status::UserError(
                    "Cannot extract 'message' from the JSON: {}", json);
            }

            return Status::UserError("{}", message);
        }

        MELON_ALWAYS_INLINE Status call(
            out_type<UnknownValue> &result,
            const arg_type<int32_t> & /*errorCode*/,
            const arg_type<Json> &json) {
            return call(result, json);
        }

    private:
        static simdjson::error_code extractMessage(
            const StringView &json,
            std::string_view &message) {
            simdjson::padded_string paddedJson(json.data(), json.size());

            SIMDJSON_ASSIGN_OR_RAISE(auto jsonDoc, simdjsonParse(paddedJson));

            SIMDJSON_ASSIGN_OR_RAISE(message, jsonDoc["message"].get_string());

            return simdjson::SUCCESS;
        }
    };
} // namespace kumo::pollux::functions
